<!DOCTYPE html>
<html lang="zh-cn" itemscope itemtype="http://schema.org/WebPage">
  <head>
    

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

  <title>Headless service - panda power!</title>
  <meta name="description" content="kubernetes之Headless 1、简介 service的cluster ip工作原理：kube-proxy 会监视 kubernetes 控制节点对 service 对象和 endpoints 对象的添加和移除。对每个 service，它会添加iptables 规则，从而捕获到达该 Service 的 clusterIP 和端口的请求，进而将请求重定向到 service 任意一组 backend pod 中; 对于每个 endpoints 对象，它也会添加iptables规则，这个规则会选择一个 backend pod 组合
headless service也是一种service，不同的是不会为headless service分配cluster ip，没有cluster ip，kube-proxy 并不处理此类服务，而且kubernetes也不会为它们进行负载均衡和路由
2、service和headless service对比 2.1为应用添加service并测试解析结果 [root@node01 headless]# cat svc-nginx.yaml apiVersion: v1 kind: Service metadata: name: nginx namespace: emiya spec: type: NodePort ports: - name: nginx protocol: TCP port: 80 nodePort: 30010 selector: app: nginx 部署并测试service name解析
如图所示，从上面的结果中我们可以看到虽然service有2个endpoint，但是dns查询时只会返回service的cluster-ip的地址。具体client访问的是哪个real server，是由iptables来决定的">
  <meta name="author" content="casper chen"/><script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "WebSite",
    "name": "panda power!",
    
    "url": "https:\/\/cyboca.ga"
}
</script><script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Organization",
  "name": "",
  "url": "https:\/\/cyboca.ga"
  
  
  
  
}
</script>
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
        "@type": "ListItem",
        "position": 1,
        "item": {
          "@id": "https:\/\/cyboca.ga",
          "name": "home"
        }
    },{
        "@type": "ListItem",
        "position": 3,
        "item": {
          "@id": "https:\/\/cyboca.ga\/post\/emiya\/headless-service\/",
          "name": "Headless service"
        }
    }]
}
</script><script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Article",
  "author": {
    "name" : "emiya"
  },
  "headline": "Headless service",
  "description" : "kubernetes之Headless 1、简介 service的cluster ip工作原理：kube-proxy 会监视 kubernetes 控制节点对 service 对象和 endpoints 对象的添加和移除。对每个 service，它会添加iptables 规则，从而捕获到达该 Service 的 clusterIP 和端口的请求，进而将请求重定向到 service 任意一组 backend pod 中; 对于每个 endpoints 对象，它也会添加iptables规则，这个规则会选择一个 backend pod 组合\nheadless service也是一种service，不同的是不会为headless service分配cluster ip，没有cluster ip，kube-proxy 并不处理此类服务，而且kubernetes也不会为它们进行负载均衡和路由\n2、service和headless service对比 2.1为应用添加service并测试解析结果 [root@node01 headless]# cat svc-nginx.yaml apiVersion: v1 kind: Service metadata: name: nginx namespace: emiya spec: type: NodePort ports: - name: nginx protocol: TCP port: 80 nodePort: 30010 selector: app: nginx 部署并测试service name解析\n如图所示，从上面的结果中我们可以看到虽然service有2个endpoint，但是dns查询时只会返回service的cluster-ip的地址。具体client访问的是哪个real server，是由iptables来决定的",
  "inLanguage" : "zh-cn",
  "wordCount":  194 ,
  "datePublished" : "2019-11-24T23:40:33",
  "dateModified" : "2019-11-24T23:40:33",
  "image" : "https:\/\/cyboca.ga\/img\/avatar-icon.png",
  "keywords" : [ "Kubernetes, Headless service" ],
  "mainEntityOfPage" : "https:\/\/cyboca.ga\/post\/emiya\/headless-service\/",
  "publisher" : {
    "@type": "Organization",
    "name" : "https:\/\/cyboca.ga",
    "logo" : {
        "@type" : "ImageObject",
        "url" : "https:\/\/cyboca.ga\/img\/avatar-icon.png",
        "height" :  60 ,
        "width" :  60
    }
  }
}
</script>

<meta property="og:title" content="Headless service" />
<meta property="og:description" content="kubernetes之Headless 1、简介 service的cluster ip工作原理：kube-proxy 会监视 kubernetes 控制节点对 service 对象和 endpoints 对象的添加和移除。对每个 service，它会添加iptables 规则，从而捕获到达该 Service 的 clusterIP 和端口的请求，进而将请求重定向到 service 任意一组 backend pod 中; 对于每个 endpoints 对象，它也会添加iptables规则，这个规则会选择一个 backend pod 组合
headless service也是一种service，不同的是不会为headless service分配cluster ip，没有cluster ip，kube-proxy 并不处理此类服务，而且kubernetes也不会为它们进行负载均衡和路由
2、service和headless service对比 2.1为应用添加service并测试解析结果 [root@node01 headless]# cat svc-nginx.yaml apiVersion: v1 kind: Service metadata: name: nginx namespace: emiya spec: type: NodePort ports: - name: nginx protocol: TCP port: 80 nodePort: 30010 selector: app: nginx 部署并测试service name解析
如图所示，从上面的结果中我们可以看到虽然service有2个endpoint，但是dns查询时只会返回service的cluster-ip的地址。具体client访问的是哪个real server，是由iptables来决定的">
<meta property="og:image" content="https://cyboca.ga/img/avatar-icon.png" />
<meta property="og:url" content="https://cyboca.ga/post/emiya/headless-service/" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="panda power!" />

  <meta name="twitter:title" content="Headless service" />
  <meta name="twitter:description" content="kubernetes之Headless 1、简介 service的cluster ip工作原理：kube-proxy 会监视 kubernetes 控制节点对 service 对象和 endpoints 对象的添加和移除。对每个 service，它会添加iptables 规则，从而捕获到达该 Service 的 clusterIP 和端口的请求，进而将请求重定向到 service …">
  <meta name="twitter:image" content="https://cyboca.ga/img/avatar-icon.png" />
  <meta name="twitter:card" content="summary" />
  <link href='https://cyboca.ga/img/favicon.ico' rel='icon' type='image/x-icon'/>
  <meta name="generator" content="Hugo 0.58.3" />
  <link rel="alternate" href="https://cyboca.ga/index.xml" type="application/rss+xml" title="panda power!"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" integrity="sha384-9eLZqc9ds8eNjO3TmqPeYcDj8n+Qfa4nuSiGYa6DjLNcv9BtN69ZIulL9+8CqC9Y" crossorigin="anonymous">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"><link rel="stylesheet" href="https://cyboca.ga/css/main.css" /><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" />
  <link rel="stylesheet" href="https://cyboca.ga/css/highlight.min.css" /><link rel="stylesheet" href="https://cyboca.ga/css/codeblock.css" /><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe.min.css" integrity="sha384-h/L2W9KefUClHWaty3SLE5F/qvc4djlyR4qY3NUV5HGQBBW7stbcfff1+I/vmsHh" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/default-skin/default-skin.min.css" integrity="sha384-iD0dNku6PYSIQLyfTOpB06F2KCZJAKLOThS5HRe8b3ibhdEQ6eKsFf/EeFxdOt5R" crossorigin="anonymous">



  </head>
  <body>
    <nav class="navbar navbar-default navbar-fixed-top navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
        <span class="sr-only">切换导航</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="https://cyboca.ga">panda power!</a>
    </div>

    <div class="collapse navbar-collapse" id="main-navbar">
      <ul class="nav navbar-nav navbar-right">
        
          
            <li>
              <a title="文章" href="/categories/">文章</a>
            </li>
          
        
          
            <li>
              <a title="tags" href="/tags/">tags</a>
            </li>
          
        

        

        
      </ul>
    </div>

    
      <div class="avatar-container">
        <div class="avatar-img-border">
          <a title="panda power!" href="https://cyboca.ga">
            <img class="avatar-img" src="https://cyboca.ga/img/avatar-icon.png" alt="panda power!" />
          </a>
        </div>
      </div>
    

  </div>
</nav>




    


<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

<div class="pswp__bg"></div>

<div class="pswp__scroll-wrap">
    
    <div class="pswp__container">
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
    </div>
    
    <div class="pswp__ui pswp__ui--hidden">
    <div class="pswp__top-bar">
      
      <div class="pswp__counter"></div>
      <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
      <button class="pswp__button pswp__button--share" title="Share"></button>
      <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
      <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
      
      
      <div class="pswp__preloader">
        <div class="pswp__preloader__icn">
          <div class="pswp__preloader__cut">
            <div class="pswp__preloader__donut"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
      <div class="pswp__share-tooltip"></div>
    </div>
    <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
    </button>
    <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
    </button>
    <div class="pswp__caption">
      <div class="pswp__caption__center"></div>
    </div>
    </div>
    </div>
</div>


  
  
  






  

  <header class="header-section ">
    
    <div class="intro-header no-img">
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
            <div class="post-heading">
              
                <h1>Headless service</h1>
              
              
              
              
                <span class="post-meta">
  
  
  <i class="fas fa-calendar"></i>&nbsp;发表于 November 24, 2019
  
  
    &nbsp;|&nbsp;<i class="fas fa-clock"></i>&nbsp;1&nbsp;分钟
  
  
    &nbsp;|&nbsp;<i class="fas fa-book"></i>&nbsp;194&nbsp;个字
  
  
    
      &nbsp;|&nbsp;<i class="fas fa-user"></i>&nbsp;emiya
    
  
  
</span>


              
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>


    <div id="gotoTop"><a href="#"></a></div>
    
<div class="container" role="main">
  <div class="row">
    <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
      <article role="main" class="blog-post">
        

<h1 id="kubernetes之headless">kubernetes之Headless</h1>

<h2 id="1-简介">1、简介</h2>

<p>service的cluster ip工作原理：kube-proxy 会监视 kubernetes 控制节点对 service 对象和 endpoints 对象的添加和移除。对每个 service，它会添加iptables 规则，从而捕获到达该 Service 的 clusterIP 和端口的请求，进而将请求重定向到 service 任意一组 backend pod 中; 对于每个 endpoints 对象，它也会添加iptables规则，这个规则会选择一个 backend pod 组合</p>

<p>headless service也是一种service，不同的是不会为headless service分配cluster ip，没有cluster ip，kube-proxy 并不处理此类服务，而且kubernetes也不会为它们进行负载均衡和路由</p>

<p><img src="/img/emiya/headless-service/001.png" alt="img" /></p>

<h2 id="2-service和headless-service对比">2、service和headless service对比</h2>

<h3 id="2-1为应用添加service并测试解析结果">2.1为应用添加service并测试解析结果</h3>
<div class="highlight"><pre class="chroma">[root@node01 headless]# cat svc-nginx.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: emiya
spec:
  type: NodePort
  ports:
    - name: nginx
      protocol: TCP
      port: 80
      nodePort: 30010
  selector:
    app: nginx</pre></div>
<p>部署并测试service name解析</p>

<p><img src="/img/emiya/headless-service/002.png" alt="img" /></p>

<p>如图所示，从上面的结果中我们可以看到虽然<code>service</code>有2个<code>endpoint</code>，但是<code>dns</code>查询时只会返回<code>service</code><code>的cluster-ip</code>的地址。具体<code>client</code>访问的是哪个<code>real server</code>，是由<code>iptables</code>来决定的</p>

<h3 id="2-2为应用添加headless-service并测试解析结果">2.2为应用添加headless service并测试解析结果</h3>
<div class="highlight"><pre class="chroma">[root@node01 headless]# cat headless-svc-nginx.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: emiya
spec:
  clusterIP: None
  ports:
    - name: nginx
      protocol: TCP
      port: 80
  selector:
    app: nginx</pre></div>
<p>部署并测试解析结果</p>

<p><img src="/img/emiya/headless-service/003.png" alt="img" /></p>

<p>如图所示，创建的headless service没有cluster ip，并且解析出来的地址有两个，都是selector选择对应标签的backend pod的真实ip地址</p>

<h2 id="3-headless-service的使用场景">3、headless service的使用场景</h2>

<p>第一种：需要 client 自己来决定使用哪个real server的时候</p>

<p>第二种：需要pod之间互相访问,这样就需要每个backend pod需要有自己独立的域名</p>

<p>针对以上场景部署测试用例：</p>
<div class="highlight"><pre class="chroma">[root@node01 headless]# cat sts_nginx.yaml 
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx
  namespace: emiya
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: &#34;nginx&#34;
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 192.168.10.30:5000/emiya/nginx:animal
        resources:
          limits:
            cpu: 0.2
            memory: 200Mi
        ports:
        - containerPort: 80
[root@node01 headless]# cat headless-svc-nginx.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: emiya
spec:
  clusterIP: None
  ports:
    - name: nginx
      protocol: TCP
      port: 80
  selector:
    app: nginx</pre></div>
<p>注意事项：需要现部署headless service，并且statefulset中需要添加spec:serviceName: “servicename”</p>

<p><img src="/img/emiya/headless-service/004.png" alt="img" /></p>

<p><img src="/img/emiya/headless-service/005.png" alt="img" /></p>

<p>如上图所示，每一个单独的backend pod都有一个独立的域名地址</p>

<h2 id="4-备注">4、备注</h2>

<p>1、一般来说需要用到headless service的场景是部署集群，需要集群的每个pod能够互相访问，使用pod ip来访问是不现实的，这个时候需要固定的pod name可以用statefulset来部署，例如redis集群：<a href="https://cyboca.ga/post/emiya/kubernetes-redis-cluster/">https://cyboca.ga/post/emiya/kubernetes-redis-cluster/</a></p>


        
          <div class="blog-tags">
            
              <a href="https://cyboca.ga/tags/kubernetes/">Kubernetes</a>&nbsp;
            
              <a href="https://cyboca.ga/tags/headless-service/">Headless service</a>&nbsp;
            
          </div>
        

        
            <hr/>
            <section id="social-share">
              <div class="list-inline footer-links">
                

<div class="share-box" aria-hidden="true">
    <ul class="share">
      
      <li>
        <a href="//twitter.com/share?url=https%3a%2f%2fcyboca.ga%2fpost%2femiya%2fheadless-service%2f&amp;text=Headless%20service&amp;via=" target="_blank" title="Share on Twitter">
          <i class="fab fa-twitter"></i>
        </a>
      </li>
  
      
      <li>
        <a href="//www.facebook.com/sharer/sharer.php?u=https%3a%2f%2fcyboca.ga%2fpost%2femiya%2fheadless-service%2f" target="_blank" title="Share on Facebook">
          <i class="fab fa-facebook"></i>
        </a>
      </li>
  
      
      <li>
        <a href="//reddit.com/submit?url=https%3a%2f%2fcyboca.ga%2fpost%2femiya%2fheadless-service%2f&amp;title=Headless%20service" target="_blank" title="Share on Reddit">
          <i class="fab fa-reddit"></i>
        </a>
      </li>
  
      
      <li>
        <a href="//www.linkedin.com/shareArticle?url=https%3a%2f%2fcyboca.ga%2fpost%2femiya%2fheadless-service%2f&amp;title=Headless%20service" target="_blank" title="Share on LinkedIn">
          <i class="fab fa-linkedin"></i>
        </a>
      </li>
  
      
      <li>
        <a href="//www.stumbleupon.com/submit?url=https%3a%2f%2fcyboca.ga%2fpost%2femiya%2fheadless-service%2f&amp;title=Headless%20service" target="_blank" title="Share on StumbleUpon">
          <i class="fab fa-stumbleupon"></i>
        </a>
      </li>
  
      
      <li>
        <a href="//www.pinterest.com/pin/create/button/?url=https%3a%2f%2fcyboca.ga%2fpost%2femiya%2fheadless-service%2f&amp;description=Headless%20service" target="_blank" title="Share on Pinterest">
          <i class="fab fa-pinterest"></i>
        </a>
      </li>
    </ul>
  </div>
  

              </div>
            </section>
        

        
          
            
          

          
                  <h4 class="see-also">也可以看看</h4>
                  <ul>
                
                
                    <li><a href="/post/emiya/networkpolicy/">NetworkPolicy</a></li>
                
                    <li><a href="/post/emiya/traefik/">Traefik</a></li>
                
                    <li><a href="/post/emiya/annotations-nginx-ingress/">Annotations Nginx Ingress</a></li>
                
                    <li><a href="/post/emiya/nginx-ingress-0.21.0/">安装Nginx Ingress 0.21.0</a></li>
                
                    <li><a href="/post/emiya/kubernetes-redis-cluster/">Kubernetes Redis Cluster</a></li>
                
              </ul>

          
        
      </article>

      
        <ul class="pager blog-pager">
          
            <li class="previous">
              <a href="https://cyboca.ga/post/emiya/nginx-conf/" data-toggle="tooltip" data-placement="top" title="Nginx Conf">&larr; 前一篇</a>
            </li>
          
          
            <li class="next">
              <a href="https://cyboca.ga/post/makemon/dce%E5%86%85%E6%A0%B8%E5%8F%82%E6%95%B0%E4%BC%98%E5%8C%96%E5%90%AB%E4%B9%89/" data-toggle="tooltip" data-placement="top" title="DCE内核参数优化含义">后一篇 &rarr;</a>
            </li>
          
        </ul>
      


      
        
        
      

    </div>
  </div>
</div>

      
<footer>
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <ul class="list-inline text-center footer-links">
          
          
          <li>
            <a href="" title="RSS">
              <span class="fa-stack fa-lg">
                <i class="fas fa-circle fa-stack-2x"></i>
                <i class="fas fa-rss fa-stack-1x fa-inverse"></i>
              </span>
            </a>
          </li>
          
        </ul>
        <p class="credits copyright text-muted">
          
            
              <a href="example.com">casper chen</a>
            
          

          &nbsp;&bull;&nbsp;&copy;
          
            2019
          

          
            &nbsp;&bull;&nbsp;
            <a href="https://cyboca.ga">panda power!</a>
          
        </p>
        
        <p class="credits theme-by text-muted">
          由 <a href="https://gohugo.io">Hugo v0.58.3</a> 强力驱动 &nbsp;&bull;&nbsp; 主题 <a href="https://github.com/halogenica/beautifulhugo">Beautiful Hugo</a> 移植自 <a href="https://deanattali.com/beautiful-jekyll/">Beautiful Jekyll</a>
          
        </p>
      </div>
    </div>
  </div>
</footer><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.js" integrity="sha384-K3vbOmF2BtaVai+Qk37uypf7VrgBubhQreNQe9aGsz9lB63dIFiQVlJbr92dw2Lx" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/contrib/auto-render.min.js" integrity="sha384-kmZOZB5ObwgQnS/DuDg6TScgOiWWBiVt0plIRkZCmE6rDZGrEOQeHM5PcHi+nyqe" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<script src="https://cyboca.ga/js/main.js"></script>
<script src="https://cyboca.ga/js/highlight.min.js"></script>
<script> hljs.initHighlightingOnLoad(); </script>
<script> $(document).ready(function() {$("pre.chroma").css("padding","0");}); </script><script> renderMathInElement(document.body); </script><script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe.min.js" integrity="sha384-QELNnmcmU8IR9ZAykt67vGr9/rZJdHbiWi64V88fCPaOohUlHCqUD/unNN0BXSqy" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe-ui-default.min.js" integrity="sha384-m67o7SkQ1ALzKZIFh4CiTA8tmadaujiTa9Vu+nqPSwDOqHrDmxLezTdFln8077+q" crossorigin="anonymous"></script><script src="https://cyboca.ga/js/load-photoswipe.js"></script>









    
  </body>
</html>

